package cn.springcloud.fix.auth.granter;

import cn.springcloud.fix.auth.dao.entity.User;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

public class MobilePasswordTokenGranter extends AbstractCustomTokenGranter {

    private static final String GRANT_TYPE = "pwd";

    private final CustomUserDetailsService customUserDetailsService;

    public MobilePasswordTokenGranter(CustomUserDetailsService userDetailsService, AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
        this.customUserDetailsService = userDetailsService;
    }

    @Override
    protected User getCustomUser(Map<String, String> parameters) {
        String mobile = parameters.get("mobile");
        String password = parameters.get("password");
        return customUserDetailsService.loadUserByMobileAndPassword(mobile, password);
    }
}
